﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Windows;

namespace Teacher
{
    public partial class select_student : Form
    {
        private List<int> selectedGroupIds = new List<int>();
        public Model.t_data t_Data = new Model.t_data();
        public select_student()
        {
            InitializeComponent();
            Load_Gruop();
        }
        public void Load_Gruop()
        {
            BLL.B_Group Stu_group = new BLL.B_Group();
            dataGridView1.DataSource = Stu_group.GetList();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                // 获取当前行的数据
                var row = dataGridView1.Rows[e.RowIndex];
                textBox1.Text = row.Cells["group_id"].Value.ToString();
                textBox2.Text = row.Cells["group_name"].Value.ToString();
                textBox4.Text = row.Cells["project_info"].Value.ToString();
            }
        }

        private void c1_TextChanged(object sender, EventArgs e)
        {

        }

        private void add_g_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                var selectRow = dataGridView1.SelectedRows[0];
                int g_id = (int)dataGridView1.CurrentRow.Cells["group_id"].Value;

                if (!selectedGroupIds.Contains(g_id))
                {
                    selectedGroupIds.Add(g_id);
                    System.Windows.Forms.TextBox CurrentextBox = FindFirstEmptyTextBox();
                    if (CurrentextBox != null)
                    {
                        CurrentextBox.Text = g_id.ToString();
                    }
                    else
                    {
                        MessageBox.Show("选择已满");
                    }

                }
                else
                {
                    MessageBox.Show("该组已被选择");
                }

            }

        }
        private System.Windows.Forms.TextBox FindFirstEmptyTextBox()
        {
            if (string.IsNullOrEmpty(c1.Text))
                return c1;
            if (string.IsNullOrEmpty(c2.Text))
                return c2;
            if (string.IsNullOrEmpty(c3.Text))
                return c3;
            if (string.IsNullOrEmpty(c4.Text))
                return c4;
            if (string.IsNullOrEmpty(c5.Text))
                return c5;
            return null; // 没有找到空的TextBox

        }

        private void clear_g_Click(object sender, EventArgs e)
        {
            
            //从最后开始删
            int lastGroupId = selectedGroupIds[selectedGroupIds.Count - 1];
            selectedGroupIds.RemoveAt(selectedGroupIds.Count - 1);

            // 找到对应的TextBox并清除
            System.Windows.Forms.TextBox Current_box_clear = FindTextBoxByGroupId(lastGroupId);
            if (Current_box_clear != null)
            {
                Current_box_clear.Clear();
            }
            
        }
        //找到最后一组对应的textbox
        private System.Windows.Forms.TextBox FindTextBoxByGroupId(int groupId)
        {
            // 假设您的TextBox控件分别命名为textBox1、textBox2、textBox3、textBox4和textBox5
            if (c1.Text == groupId.ToString()) 
                return c1;
            if (c2.Text == groupId.ToString())
                return c2;
            if (c3.Text == groupId.ToString())
                return c3;
            if (c4.Text == groupId.ToString()) 
                return c4;
            if (c5.Text == groupId.ToString())
                return c5;
            return null; // 没有找到对应的TextBox
        }

        private void save_g_Click(object sender, EventArgs e)
        {
            t_Data.c1 = selectedGroupIds[0];
            t_Data.c2 = selectedGroupIds[1];
            t_Data.c3 = selectedGroupIds[2];
            t_Data.c4 = selectedGroupIds[3];
            t_Data.c5 = selectedGroupIds[4];
            t_Data.t_id = Model.log_info.teacher.teacher_id;
            BLL.B_t_data t_g_data = new BLL.B_t_data();
            bool result =t_g_data.Save_group(t_Data);
            if(result)
            {
                MessageBox.Show("提交成功");
            }
            else
            {
                MessageBox.Show("提交失败");
            }
        }
    }
}
